/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.st.service;

import java.util.List;

import com.jeesite.modules.st.dao.StInventoryBillDetailDao;
import com.jeesite.modules.st.entity.StInventoryBillDetail;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jeesite.common.entity.Page;
import com.jeesite.common.service.CrudService;
import com.jeesite.modules.st.entity.StCheckLog;
import com.jeesite.modules.st.dao.StCheckLogDao;
import com.jeesite.modules.st.entity.StCheckLogDetail;
import com.jeesite.modules.st.dao.StCheckLogDetailDao;

/**
 * 进料检验Service
 *
 * @author HOHD
 * @version 2020-12-16
 */
@Service
@Transactional(readOnly = true)
public class StCheckLogService extends CrudService<StCheckLogDao, StCheckLog> {

    @Autowired
    private StCheckLogDetailDao stCheckLogDetailDao;

    /**
     * 获取单条数据
     *
     * @param stCheckLog
     * @return
     */
    @Override
    public StCheckLog get(StCheckLog stCheckLog) {
        StCheckLog entity = super.get(stCheckLog);
        if (entity != null) {
            StCheckLogDetail stCheckLogDetail = new StCheckLogDetail(entity);
            stCheckLogDetail.setStatus(StCheckLogDetail.STATUS_NORMAL);
            entity.setStCheckLogDetailList(stCheckLogDetailDao.findList(stCheckLogDetail));
        }
        return entity;
    }

    /**
     * 查询分页数据
     *
     * @param stCheckLog      查询条件
     * @param stCheckLog.page 分页对象
     * @return
     */
    @Override
    public Page<StCheckLog> findPage(StCheckLog stCheckLog) {
        return super.findPage(stCheckLog);
    }


    @Autowired
    StInventoryBillDetailDao stInventoryBillDetailDao;

    /**
     * 保存数据（插入或更新）
     *
     * @param stCheckLog
     */
    @Override
    @Transactional(readOnly = false)
    public void save(StCheckLog stCheckLog) {
        String inventoryBillDetailId = stCheckLog.getInventoryBillDetailId();
        StInventoryBillDetail stInventoryBillDetail = new StInventoryBillDetail();
        stInventoryBillDetail.setId(inventoryBillDetailId);
        stInventoryBillDetail = stInventoryBillDetailDao.get(stInventoryBillDetail);
		Integer checkState = stCheckLog.getCheckState();
		stInventoryBillDetail.setCheckState(checkState);
		stInventoryBillDetailDao.update(stInventoryBillDetail);
		super.save(stCheckLog);
        // 保存 StCheckLog子表
        for (StCheckLogDetail stCheckLogDetail : stCheckLog.getStCheckLogDetailList()) {
            if (!StCheckLogDetail.STATUS_DELETE.equals(stCheckLogDetail.getStatus())) {
                stCheckLogDetail.setParentId(stCheckLog);
                if (stCheckLogDetail.getIsNewRecord()) {
                    stCheckLogDetailDao.insert(stCheckLogDetail);
                } else {
                    stCheckLogDetailDao.update(stCheckLogDetail);
                }
            } else {
                stCheckLogDetailDao.delete(stCheckLogDetail);
            }
        }
    }

    /**
     * 更新状态
     *
     * @param stCheckLog
     */
    @Override
    @Transactional(readOnly = false)
    public void updateStatus(StCheckLog stCheckLog) {
        super.updateStatus(stCheckLog);
    }

    /**
     * 删除数据
     *
     * @param stCheckLog
     */
    @Override
    @Transactional(readOnly = false)
    public void delete(StCheckLog stCheckLog) {
        super.delete(stCheckLog);
        StCheckLogDetail stCheckLogDetail = new StCheckLogDetail();
        stCheckLogDetail.setParentId(stCheckLog);
        stCheckLogDetailDao.deleteByEntity(stCheckLogDetail);
    }

}